package ru.zombator.taskman.client.actions;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Shell;

import ru.zombator.taskman.client.ui.ClientWindow;
import ru.zombator.taskman.client.ui.dialogs.CreateProjectDialog;
import ru.zombator.taskman.core.domain.ITaskProject;
import ru.zombator.taskman.core.query.payload.CreateProjectRequest;
import ru.zombator.taskman.core.query.payload.IPayload;
import ru.zombator.taskman.core.query.payload.ObjectCreatedResponse;

/**
 * Действие "создать проект"
 * 
 * @author nvamelichev
 * 
 */
public class CreateProjectAction extends ADialogAction {
    public CreateProjectAction(ClientWindow mainWnd) {
        super(mainWnd);
    }
    
    @Override
    public String getText() {
        return "Создать...";
    }
    
    @Override
    protected Dialog createDialog(Shell parentShell) {
        return new CreateProjectDialog(parentShell);
    }
    
    @Override
    protected IPayload createRequest(Dialog dlg) {
        CreateProjectDialog cpDlg = (CreateProjectDialog) dlg;
        return new CreateProjectRequest(cpDlg.getName(), cpDlg.getPassword());
    }
    
    @Override
    protected String processResponse(IPayload response) {
        ObjectCreatedResponse ocr = (ObjectCreatedResponse) response;
        ITaskProject project = (ITaskProject) ocr.getCreatedObject();
        return String.format("Проект \"%s\" успешно создан (ID=%s)", project.getName(), project
                .getId().toString());
    }
}
